//
// Created by dong on 2025/10/20.
//

#include "SLabel.h"

#include "SPaint.h"
#include "Swindow.h"

SLabel::SLabel(const string& text)
{
    m_font="../simkai.ttf";
    m_text=text;
    m_size=25;
    m_type = LabelType::TextLabel;
    m_data->textColor={0,0,0,0};
    m_data->w = 0;
    m_data->h = 0;
}

SLabel::SLabel(const string& text, const string& url)
{

    img = IMG_Load(url.c_str());
    imageUrl = url;
    m_text = text;
    m_data->w = 0;
    m_data->h = 0;
    m_type = LabelType::ImgLabel;
}

void SLabel::paintEvent(SDL_Event* msg)
{
    if (m_type==LabelType::TextLabel)
    {
    // TTF_Font* font = TTF_OpenFont(m_font.c_str(),m_size);
    // SDL_Surface* surface=TTF_RenderUTF8_Blended(font,m_text.c_str(),m_data->textColor);
    // SDL_Texture* temp = SDL_CreateTextureFromSurface(Swindow::render,surface);
    // SDL_Rect dst={m_data->x,m_data->y};
    // if (m_data->w==0 | m_data->h==0)
    // {
    //     dst.w = surface->w;
    //     dst.h = surface->h;
    // }else
    // {
    //     dst.w =m_data->w;
    //     dst.h = m_data->h;
    // }
    //
    // SDL_RenderCopy(Swindow::render,temp,NULL,&dst);
    // // SDL_FreeSurface(surface);
    // TTF_CloseFont(font);

        SPaint spaint;
        spaint.drawText(m_data->x,m_data->y,m_text.c_str());
    }else if (m_type==LabelType::ImgLabel)
    {
        if (img!=nullptr)
        {
            SDL_Texture* temp = SDL_CreateTextureFromSurface(Swindow::render,img);
            SDL_Rect dst={m_data->x,m_data->y};
            if (m_data->w==0 | m_data->h==0)
            {
                dst.w = img->w;
                dst.h = img->h;
            }else
            {
                dst.w =m_data->w;
                dst.h = m_data->h;
            }

            // SDL_RenderCopy(Swindow::render,temp,NULL,&dst);
            // SDL_FreeSurface(img);

            SPaint spaint;
            spaint.drawImg(dst,imageUrl);
        }
    }
}

void SLabel::setfont(const string& font)
{
    m_font=font;
}

void SLabel::settext(const string& text)
{
    m_text=text;
}

void SLabel::setsize(const int& size)
{
    m_size = size;
}

void SLabel::setsize(int w, int h)
{
    m_data->w =w;
    m_data->h = h;
}
